//
//  APPRefresh.swift
//  APP
//
//  Created by Rdxer on 2017/12/22.
//  Copyright © 2017年 Rdxer. All rights reserved.
//

import UIKit
import MJRefresh

extension UIScrollView{
    
    /// 头部 下拉刷新
    @discardableResult
    func prepareRefreshHeader(refreshingTarget: Any, refreshingAction: Selector)->APHRefreshHeader{
        let res = APHRefreshHeader.init(refreshingTarget: refreshingTarget, refreshingAction: refreshingAction)
        self.mj_header = res
        return res!
    }
    
    
    /// 底部 上拉加载更多
    @discardableResult
    func prepareRefreshFooter(refreshingTarget: Any, refreshingAction: Selector)->APHRefreshAutoFooter{
        let res = APHRefreshAutoFooter.init(refreshingTarget: refreshingTarget, refreshingAction: refreshingAction)
        self.mj_footer = res
        return res!
    }
}

@objc
protocol XXPrepareRefreshHeaderProtocol :NSObjectProtocol{
    @objc optional func loadData()
}
@objc
protocol XXPrepareRefreshFooterProtocol :NSObjectProtocol{
    @objc optional func loadMoreData()
}

extension XXPrepareRefreshHeaderProtocol {
    /// 头部 下拉刷新
    @discardableResult
    public func prepareRefreshHeader(_ scrollView:UIScrollView)->APHRefreshHeader?{
        if self.responds(to: #selector(loadData)) {
            return scrollView.prepareRefreshHeader(refreshingTarget: self, refreshingAction: #selector(loadData))
        }
        return nil
    }
}

extension XXPrepareRefreshFooterProtocol {
    /// 底部 上拉加载更多
    @discardableResult
    public func prepareRefreshFooter(_ scrollView:UIScrollView)->APHRefreshAutoFooter?{
        if self.responds(to: #selector(loadMoreData)) {
            return scrollView.prepareRefreshFooter(refreshingTarget: self, refreshingAction: #selector(loadMoreData))
        }
        return nil
    }
}

@objc
protocol XXPrepareRefreshHeaderAndFooterProtocol:XXPrepareRefreshHeaderProtocol , XXPrepareRefreshFooterProtocol {
    
}

extension XXPrepareRefreshHeaderAndFooterProtocol{
    public func prepareRefreshHeaderFooterIfNeed(_ scrollView:UIScrollView)->(
        APHRefreshHeader?,
        APHRefreshAutoFooter?
        ){
        return (
            prepareRefreshHeader(scrollView),
            prepareRefreshFooter(scrollView)
        )
    }
}

extension UITableViewController:XXPrepareRefreshHeaderAndFooterProtocol{
    
}




